package gl.android.biyiqing.competent;

import gl.adapter.SimpleAdapter.OnItemChildViewClick;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.event.EventSendGift;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.GsonUtils;
import gl.uitls.Tip;

import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.huewu.pla.lib.internal.PLA_AdapterView;

import de.greenrobot.event.EventBus;

/**
 * 选择礼物发送
 * 
 * @author geliang
 * 
 */
public class OnSelectGiftToSendItemClickListener implements
		OnItemClickListener,
		com.huewu.pla.lib.internal.PLA_AdapterView.OnItemClickListener,
		OnItemChildViewClick {

	private String jsonArrayData;
	private Object[] pars;
	private String tuserid;
	private String message;

	public OnSelectGiftToSendItemClickListener(String jsonArrayData,
			String tuserid, String message) {
		this.tuserid = tuserid;
		this.message = message;
		this.jsonArrayData = jsonArrayData;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		goUserInfoActivity(arg2);
	}

	private String NOTFOUND = "积分不够,请充值";
	private String ERR = "发送失败,请重试";
	private String NOTBUY = "暂时不能购买";

	private void goUserInfoActivity(int arg2) {
		try {
			if (tuserid == null || TextUtils.isEmpty(tuserid.toString())) {
				Tip.show(NOTBUY);
				return;
			}
			List<HashMap<String, String>> map = GsonUtils
					.JSON2MapArrayList(new JSONArray(jsonArrayData));
			if (message == null || TextUtils.isEmpty(message.toString())) {
				message = map.get(arg2).get("intro");
			}
			pars = new String[] { map.get(arg2).get("giftid"),
					UserManger.getInstance().userid, tuserid,
					map.get(arg2).get("points"), message };
			BaseControl
					.get(API.URL
							+ "SendGift?giftid=%s&fuserid=%s&tuserid=%s&points=%s&message=%s",
							new AjaxCallBackProxy() {
								@Override
								public void onStart() {
									super.onStart();
									Tip.show("正在请求服务器");
								}

								@Override
								public void onSuccess(String t) {
									super.onSuccess(t);
									try {
										if ("1".equals(new JSONObject(t)
												.getString("msg"))) {
											Tip.show(NOTFOUND);
											return;
										}
										if ("2".equals(new JSONObject(t)
												.getString("msg"))) {
											Tip.show("赠送成功");
											EventBus.getDefault().post(new EventSendGift());
											return;
										}
									} catch (Exception e) {
										e.printStackTrace();
										onFailure(e, 0, e.getMessage());
									}
								}

								@Override
								public void onFailure(Throwable t, int errorNo,
										String strMsg) {
									super.onFailure(t, errorNo, strMsg);
									Tip.show(ERR);
								}
							}, pars);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onItemClick(PLA_AdapterView<?> parent, View view, int position,
			long id) {
		goUserInfoActivity(position);
	}

	@Override
	public void onClick(View view, int position) {
		goUserInfoActivity(position);

	}

}
